﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls.Primitives;
using SqueezeboxLib;

namespace SqueezeBoxClient
{
    /// <summary>
    /// Interaction logic for PlayerWindow.xaml
    /// </summary>
    public partial class PlayerWindow : Window
    {
        private bool isSliderDragStarted;
        private readonly SqueezeboxPlayer player;

        public PlayerWindow(SqueezeboxPlayer player)
        {
            InitializeComponent();

            this.player = player;

            Title = player.ToString();

            //sliderVolume.Value = player.MixerVolume;

            labelTitle.Content = player.CurrentSong.Title;
            labelArtist.Content = player.CurrentSong.Artist;
            labelAlbum.Content = player.CurrentSong.Album;
            imageCover.Source = player.GetCurrentCoverArt();

            player.Playlist.MessageReceived += PlaylistMessageReceived;

            player.MessageReceived += OnPlayerMessageReceived;
            player.MixerVolumeChanged += OnPlayerMixerVolumeChanged;


            player.Playlist.RequestAlbum();
            player.Playlist.RequestArtist();
            player.Playlist.RequestTitle();
        }

        void OnPlayerMessageReceived(object sender, MessageReceivedEventArgs e)
        {
           
            Dispatcher.BeginInvoke((Action)(() =>
            {
                string[] split = e.Message.Split(' ');
                if(split[1] =="duration")
                {
                    slider1.Maximum = double.Parse(split[2], CultureInfo.InvariantCulture);
                }
            }));
        }

        void OnPlayerMixerVolumeChanged(object sender, SqueezeboxPlayerMixerVolumeChangedEventArgs e)
        {
            System.Diagnostics.Debug.Print("[PLAYER] Mixer Volume Changed: " + e.MixerVolume);

            Dispatcher.BeginInvoke((Action)(() =>
                                                {
                                                    if (!isSliderDragStarted)
                                                    {
                                                        sliderVolume.Value = e.MixerVolume;
                                                    }
                                                }));
        }

        private void PlaylistMessageReceived(object sender, PlaylistMessageReceivedEventArgs args)
        {
            switch (args.Command)
            {
                case PlaylistCommand.unknown:
                    break;
                case PlaylistCommand.play:
                    break;
                case PlaylistCommand.add:
                    break;
                case PlaylistCommand.insert:
                    break;
                case PlaylistCommand.deleteitem:
                    break;
                case PlaylistCommand.move:
                    break;
                case PlaylistCommand.delete:
                    break;
                case PlaylistCommand.preview:
                    break;
                case PlaylistCommand.resume:
                    break;
                case PlaylistCommand.save:
                    break;
                case PlaylistCommand.loadalbum:
                    break;
                case PlaylistCommand.addalbum:
                    break;
                case PlaylistCommand.loadtracks:
                    break;
                case PlaylistCommand.addtracks:
                    break;
                case PlaylistCommand.insertalbum:
                    break;
                case PlaylistCommand.deletealbum:
                    break;
                case PlaylistCommand.clear:
                    break;
                case PlaylistCommand.zap:
                    break;
                case PlaylistCommand.playlistinfo:
                    break;
                case PlaylistCommand.name:
                    break;
                case PlaylistCommand.url:
                    break;
                case PlaylistCommand.modified:
                    break;
                case PlaylistCommand.tracks:
                    break;
                case PlaylistCommand.index:
                    break;
                case PlaylistCommand.genere:
                    break;
                case PlaylistCommand.artist:
                    Dispatcher.BeginInvoke((Action)(() =>
                                                         {
                                                             labelArtist.Content = args.Result;
                                                         }));
                    break;
                case PlaylistCommand.album:
                    Dispatcher.BeginInvoke((Action)(() =>
                                                         {
                                                             labelAlbum.Content = args.Result;
                                                         }));
                    break;
                case PlaylistCommand.title:
                    Dispatcher.BeginInvoke((Action)(() =>
                                                         {
                                                             labelTitle.Content = args.Result;
                                                         }));
                    break;
                case PlaylistCommand.path:
                    break;
                case PlaylistCommand.remote:
                    break;
                case PlaylistCommand.duration:

                    break;
                case PlaylistCommand.shuffle:
                    break;
                case PlaylistCommand.repeat:
                    break;
                case PlaylistCommand.playlistcontrol:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void buttonPrev_Click(object sender, RoutedEventArgs e)
        {
            player.Button(SqueezeboxButtonCode.jump_rew);
        }

        private void buttonPlay_Click(object sender, RoutedEventArgs e)
        {
            player.Play(0);
        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            player.Stop();
        }

        private void buttonNext_Click(object sender, RoutedEventArgs e)
        {
            player.Button(SqueezeboxButtonCode.jump_fwd);
        }

        private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!isSliderDragStarted)
            {
                player.MixerVolume(sliderVolume.Value.ToString());
            }
        }

        private void MySlider_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            player.MixerVolume(sliderVolume.Value.ToString());
            isSliderDragStarted = false;
        }

        private void sliderVolume_DragStarted(object sender, DragStartedEventArgs e)
        {
            isSliderDragStarted = true;
        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            double time = slider1.Value;
            player.Time(time.ToString());
        }
    }
}
